package org.example;

import cn.hutool.core.io.FileUtil;
import com.google.common.base.Throwables;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {

        Scanner scanner = new Scanner(System.in); // 创建一个Scanner对象

        System.out.print("请输入tables所有目录的绝对路径：");
        String tablesDir = scanner.nextLine();
        System.out.println("解析表的路径为：" + tablesDir);

        System.out.print("请输入结果SQL文件的保存路径：");
        String savaDir = scanner.nextLine();
        System.out.println("结果文件保存路径为：" + savaDir);

        System.out.print("请输入原币种：");
        String oriCcy = scanner.nextLine();
        System.out.println("原币种为：" + oriCcy);

        System.out.print("请输入目标币种：");
        String targetCCy = scanner.nextLine();
        System.out.println("目标币种为：" + targetCCy);

        System.out.print("请输入法人代码：");
        String lglPernCode = scanner.nextLine();
        System.out.println("法人代码为：" + lglPernCode);

        scanner.close(); // 关闭Scanner对象

        // 结果
        List<String> resSql = getResSql(tablesDir, oriCcy, targetCCy, lglPernCode);

        // 将结果写文件
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(savaDir))) {
            for (String str : resSql) {
                writer.write(str + "\n"); // 将每个字符串写入文件
            }
        }
//
//        Map<String, String> fileDir = new HashMap<>();
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\ac-parent\\ac-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\ac.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\ap-parent\\ap-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\ap.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\cf-parent\\cf-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\cf.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\cm-parent\\cm-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\cm.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\cr-parent\\cr-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\cr.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\cs-parent\\cs-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\cs.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\et-parent\\et-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\et.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\is-parent\\is-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\is.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\ln-parent\\ln-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\ln.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\lt-parent\\lt-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\lt.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\mc-parent\\mc-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\mc.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\ms-parent\\ms-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\ms.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\od-parent\\od-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\od.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\or-parent\\or-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\or.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\pa-parent\\pa-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\pa.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\pc-parent\\pc-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\pc.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\pt-parent\\pt-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\pt.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\rc-parent\\rc-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\rc.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\st-parent\\st-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\st.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\ta-parent\\ta-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\ta.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\tf-parent\\tf-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\tf.txt");
//        fileDir.put("D:\\sunline\\code\\workspace\\back\\Mcore\\uc-parent\\uc-base\\src\\main\\resources\\tables", "C:\\Users\\zyl\\Downloads\\resSql\\uc.txt");

//        for (Entry<String, String> entry : fileDir.entrySet()) {
//            String tablesDir = entry.getKey();
//            String savaDir = entry.getValue();
//
//            // 结果
//            List<String> resSql = getResSql(tablesDir);
//
//            // 将结果写文件
//            try (BufferedWriter writer = new BufferedWriter(new FileWriter(savaDir))) {
//                for (String str : resSql) {
//                    writer.write(str + "\n"); // 将每个字符串写入文件
//                }
//            }
//        }


    }

    static List<String> getResSql(String dir, String oriCcy, String targetCCy, String lglPernCode) {
        List<File> files = FileUtil.loopFiles(dir, file -> file.getName().endsWith("xml"));

        // 将表名及字段id保存到map中
        Map<String, List<String>> resMap = new HashMap<>();

        for (File file : files) {

            // 排除jarTable目录和target目录
            String fileAbsolutePath = file.getAbsolutePath();
            if (fileAbsolutePath.contains("jartable") || fileAbsolutePath.contains("target") || fileAbsolutePath.contains("Impl") || !fileAbsolutePath.endsWith(".tables.xml")) {
                continue;
            }

            // 创建XML读取器
            SAXReader reader = new SAXReader();

            try {
                // 读取XML文件
                Document document = reader.read(file);


                // 遍历该xml文件的所有table节点
                List<Element> tables = document.getRootElement().elements("table");
                for (Element table : tables) {

                    // 获取当前table节点的id属性值即表名
                    String tableName = table.attributeValue("id");

                    // 遍历该表的所有字段
                    List<Element> fields = table.element("fields").elements("field");
                    for (Element field : fields) {
                        // 获取当前节点的type属性值
                        String type = field.attributeValue("type");

                        // 如果type=BaseEnumType.E_CCY_CODE，则保存所需信息到map中
                        if ("BaseEnumType.E_CCY_CODE".equals(type)) {
                            String fieldId = field.attributeValue("id");

                            if (resMap.get(tableName) == null) {
                                ArrayList<String> fields1 = new ArrayList<>();
                                fields1.add(fieldId);
                                resMap.put(tableName, fields1);
                            } else {
                                resMap.get(tableName).add(fieldId);
                            }
                        }
                    }
                }


            } catch (Exception e) {
                System.out.println(Throwables.getStackTraceAsString(e));
            }

        }

        List<String> sqlList = new ArrayList<>();
        // 遍历map并生成sql
        for (Map.Entry<String, List<String>> entry : resMap.entrySet()) {
            String tableName = entry.getKey(); // 获取表名
            List<String> fields = entry.getValue(); // 获取字段

            for (String field : fields) {
                String updSql = String.format("update %s set %s = '%s' where %s = '%s' and lgl_pern_code = '%s';", tableName, field,targetCCy, field, oriCcy, lglPernCode);
                sqlList.add(updSql);
            }
        }

        return sqlList;
    }

}